﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Text.RegularExpressions;

namespace Haidou.Wpf.Util.Converters;

	public class MacCalc
	{
		// 判断MAC地址是否在指定范围内
		public static bool IsMACAddressInRange(string macAddress, string rangeStart, string rangeEnd)
		{
			// 将MAC地址转换为BigInteger类型，便于比较
			BigInteger macAddressBigInt = ConvertMACToBigInteger(macAddress);
			BigInteger startBigInt = ConvertMACToBigInteger(rangeStart);
			BigInteger endBigInt = ConvertMACToBigInteger(rangeEnd);

			// 检查MAC地址是否在范围的起始值和结束值之间
			return macAddressBigInt >= startBigInt && macAddressBigInt <= endBigInt;
		}

		// 将MAC地址转换为BigInteger类型
		private static BigInteger ConvertMACToBigInteger(string macAddress)
		{
			try
			{
				string hexValue = macAddress.Replace(":", "");
				///表示系统。全球化。全球化. number styles . allowtrailingwhite，
				//和系统。使用globalization . number styles . allowhexspecifier样式。这
				//是复合数字样式。
				return BigInteger.Parse(hexValue, NumberStyles.HexNumber);
			}
			catch (Exception ex)
			{
				LogWriter.Log(ex, "ConvertMACToBigInteger");
				return BigInteger.Zero;
			}

		}

		// 
		public static bool Main(string mac)
		{
			string rangeStart = "A6:00:00:00:17:78";
			string rangeEnd = "A6:00:00:00:1B:61";

			string macadd = ReverseAndExtractMacAddress(mac);
			if (macadd.Contains("notLength"))
			{
				return false;
			}
			bool result = IsMACAddressInRange(macadd, rangeStart, rangeEnd);
			Debug.WriteLine($"The MAC address '{macadd}' is within the range from '{rangeStart}' to '{rangeEnd}': {result}");

			return result;
		}


		public static string ReverseAndExtractMacAddress(string inputString)
		{
			try
			{
				if (inputString == null)
				{

					return "notLength";
				}

				// 检查输入是否包含正确的mac地址长度，这里是17个字符
				if (inputString.Length != 18)
				{

					return "notLength";
				}


				// 使用Substring从指定位置开始截取，这里前8位是正常的顺序
				string normalOrderPart = inputString.Substring(6, 12);


				string macAddress = ConvertHexString(normalOrderPart);
				Debug.WriteLine(macAddress); // Output: 6A:00:00:51:9C

				//十进制参考
				//var decimals = ConvertMacToDecimal(macAddress);

				return macAddress;
			}
			catch (Exception)
			{
				return "notLength";
			}
		}

		public static string ConvertHexString(string hexString)
		{
			// 移除非16进制字符
			string cleanedHex = Regex.Replace(hexString, "[^A-Fa-f0-9]", "");

			// 从右到左每2个字符分组并添加冒号
			string formattedHex = string.Join(":",
				Enumerable.Range(0, cleanedHex.Length)
						  .Where(i => i % 2 == 0)
						  .Select(i => cleanedHex.Substring(i, 2))
						  .Reverse());

			// 如果原始字符串长度为奇数，在最后一组前面加上冒号
			if (hexString.Length % 2 == 1)
			{
				formattedHex = ":" + formattedHex;
			}

			return formattedHex;
		}

		// 将MAC地址转换为十进制表示
		public static string ConvertMacToDecimal(string macAddress)
		{
			// 检查MAC地址格式是否正确
			if (macAddress.Length != 17 || macAddress[2] != ':' || macAddress[5] != ':' || macAddress[8] != ':' || macAddress[11] != ':' || macAddress[14] != ':')
			{
				throw new ArgumentException("Invalid MAC address format.");
			}

			// 分割MAC地址为各个字节
			string[] bytes = macAddress.Split(':');
			string decimalValue = "";

			// 将每个字节转换为十进制
			for (int i = 0; i < bytes.Length; i++)
			{
				decimalValue += Convert.ToInt32(bytes[i], 16).ToString() + ":";
			}

			return decimalValue;
		}

	}

